package com.stgy.lease.web.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stgy.lease.common.utils.JwtUtil;
import com.stgy.lease.model.domain.ApartmentInfo;
import com.stgy.lease.model.domain.GraphInfo;
import com.stgy.lease.model.domain.LabelInfo;
import com.stgy.lease.model.domain.ViewAppointment;
import com.stgy.lease.web.api.mapper.ApartmentInfoMapper;
import com.stgy.lease.web.api.mapper.GraphInfoMapper;
import com.stgy.lease.web.api.mapper.LabelInfoMapper;
import com.stgy.lease.web.api.mapper.ViewAppointmentMapper;
import com.stgy.lease.web.api.service.ApartmentInfoService;
import com.stgy.lease.web.api.service.GraphInfoService;
import com.stgy.lease.web.api.service.ViewAppointmentService;
import com.stgy.lease.web.api.vo.ApartmentDetailVo;
import com.stgy.lease.web.api.vo.ApartmentItemVo;
import com.stgy.lease.web.api.vo.AppointmentDetailVo;
import com.stgy.lease.web.api.vo.AppointmentItemVo;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lingx
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:30
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    //查询个人预约看房列表
    @Override
    public List<AppointmentItemVo> listItem(String token, HttpServletRequest request) {
        Map<String, Object> map = JwtUtil.parseToken(token, request);
        Long userId = Long.parseLong(map.get("userId").toString());
        List<ViewAppointment> viewAppointments = this.list(Wrappers.lambdaQuery(ViewAppointment.class)
                .eq(ViewAppointment::getUserId, userId));
        List<AppointmentItemVo> appointmentItemVoList =
                viewAppointments.stream().map(viewAppointment -> {
                    AppointmentItemVo appointmentItemVo = new AppointmentItemVo();
                    BeanUtils.copyProperties(viewAppointment, appointmentItemVo);
                    List<GraphInfo> graphInfoList =
                            graphInfoMapper.selectByApartmentId(viewAppointment.getApartmentId());
                    appointmentItemVo.setGraphVoList(graphInfoList);
                    return appointmentItemVo;
                }).collect(Collectors.toList());
        return appointmentItemVoList;
    }

    //根据ID查询预约详情信息
    @Override
    public AppointmentDetailVo getDetailById(Long id) {
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        ViewAppointment viewAppointment = this.getById(id);
        BeanUtils.copyProperties(viewAppointment, appointmentDetailVo);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo =
                apartmentInfoMapper.selectById(viewAppointment.getApartmentId());
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);

        List<LabelInfo> labelInfos =
                labelInfoMapper.selectByApartmentId(apartmentInfo.getId());
        apartmentItemVo.setLabelInfoList(labelInfos);

        List<GraphInfo> graphInfoList =
                graphInfoMapper.selectByApartmentId(apartmentInfo.getId());
        apartmentItemVo.setGraphVoList(graphInfoList);

        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);

        return appointmentDetailVo;
    }
}